package com.nsgf.library.ui.page.manager;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.databinding.ActivityNsgfAlreadyBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.XFragmentAdapter;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.MessageActivity;
import com.nsgf.library.utils.PublicMethodUtils;
import com.nsgf.library.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单管理已办详情
 */
public class WorkOrderAlreadyActivity extends BaseActivity {

    MessageActivity.MessageStates states;

    private List<Fragment> fragmentList = new ArrayList<>();

    private String[] mPageTitleList;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {

        states = getActivityScopeViewModel(MessageActivity.MessageStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_already, BR.vm, states);
    }

    private ActivityNsgfAlreadyBinding mBinding;

    private int isResponsibleBy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = (ActivityNsgfAlreadyBinding) getBinding();
        getLifecycle().addObserver(mAccountRequester);

        states.titleBar.set(getIntent().getStringExtra("title"));
        isResponsibleBy = getIntent().getIntExtra("isResponsibleBy", 0);
        getController().displayLoading();
        mAccountRequester.patrolDetail(getIntent().getStringExtra("orderCode"));

        mAccountRequester.getWorkOrderDetail().observe(this, workOrderDetailBeanDataResult -> {

            getController().dismissLoading();
            if (workOrderDetailBeanDataResult.getResponseStatus().isSuccess()) {

                if (200 == workOrderDetailBeanDataResult.getResult().getCode()) {

                    if (!workOrderDetailBeanDataResult.getResult().getData().getAbnormalList().isEmpty()) {
                        fragmentList.add(QueryAbnormalFragment
                                .newInstance(workOrderDetailBeanDataResult.getResult().getData().getAbnormalList()));
                        fragmentList.add(WorkOrderAlreadyRecordFragment.newInstance(workOrderDetailBeanDataResult.getResult().getData()));
                        fragmentList.add(MessageAbnormalOpinionFragment.newInstance(isResponsibleBy, workOrderDetailBeanDataResult.getResult().getData()));
                        mPageTitleList = new String[]{"异常问题", "巡查记录", "审批意见"};
                        mBinding.viewPager.setOffscreenPageLimit(3);
                    } else {
                        mBinding.viewPager.setOffscreenPageLimit(2);
                        fragmentList.add(WorkOrderAlreadyRecordFragment.newInstance(workOrderDetailBeanDataResult.getResult().getData()));
                        fragmentList.add(MessageAbnormalOpinionFragment.newInstance(isResponsibleBy, workOrderDetailBeanDataResult.getResult().getData()));
                        mPageTitleList = new String[]{"巡查记录", "审批意见"};
                    }
                    XFragmentAdapter adapter = new XFragmentAdapter(this, getSupportFragmentManager(), fragmentList, mPageTitleList);
                    mBinding.viewPager.setAdapter(adapter);
                    PublicMethodUtils.initMagicIndicator(mBinding.magicIndicator, mBinding.viewPager, mPageTitleList, this);

                } else {
                    if (401 == workOrderDetailBeanDataResult.getResult().getCode()) {
                        UserInfo.getInstance().clearUserInfo();
                        finish();
                    }
                    ToastUtils.showShortToast(workOrderDetailBeanDataResult.getResult().getMsg());
                }
            } else {
                ToastUtils.showShortToast(getErrorMsg(workOrderDetailBeanDataResult.getResponseStatus().getE()));
            }
        });


    }

}
